package com.zrt.bookstore3.Dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zrt.bookstore3.Domain.Book;
import com.zrt.bookstore3.Domain.Order;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface OrderDao  extends BaseMapper<Order> {

    @Update("update my_bookstore.order set status = #{status} , pay_time = #{payTime} , telephone = #{telephone} , address = #{address} , recipient = #{recipient} where id =#{id}")
    int updateOrder(Order order);

    List<Order> selectByCondition(Order order);

    @Update("update my_bookstore.order set number = #{number} , status = #{status} where id = #{id}")
    int updateStatusAndNumber(Order order);

    @Select("select * from my_bookstore.order where id = #{id}")
    Order selectById(int id);

    @Delete("delete from my_bookstore.order where id = #{id}")
    int deleteById(int id);

    @Select("select * from my_bookstore.order")
    List<Order> selectAllData();

    @Select("select * from my_bookstore.order where uid = #{uid} and status = 1")
    List<Order> selectAllPay(int uid);

    @Update("update my_bookstore.order set status = #{status} , pay_time = #{payTime} where id =#{id}")
    int updateStatusAndTime(int status,int id,String payTime);

    @Update("update my_bookstore.order set status = #{status} where id =#{id}")
    int updateStatus(int status,int id);

    @Select("select * from my_bookstore.order where uid = #{uid} and status = 0")
    List<Order> selectByUid(int uid);

    @Insert("insert into my_bookstore.order values (0,#{number},#{createTime},#{price},#{bookId},#{name},#{uid},#{status},#{payTime},#{telephone},#{address},#{recipient})")
    int insert(Order order);

    @Select("select * from user")
    @Results({
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "number",column = "number"),
            @Result(property = "createTime",column = "create_time"),
            @Result(property = "bookList",column = "id",
                    javaType = List.class,
                    many = @Many(select =
                            "com.zrt.bookstore3.Dao.BookDao.findByOid"))
    })
    List<Order> findAllOrdersAndBooks();

    @Select("select * from my_bookstore.order where book_id = #{bookId}")
    List<Order> findByBookId(int bookId);
}
